package net.wofly.cashier.table.web;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.cashier.table.domain.Table;
import net.wofly.cashier.table.service.ITableService;
import net.wofly.cashier.table.validator.TableValidator;
import net.wofly.common.util.ReturnBean;
import net.wofly.common.web.BaseController;

@RestController
@RequestMapping(value = "/api/table")
@Api(description = "餐桌管理接口")
public class TableController extends BaseController {

	@Autowired
	ITableService iTableService;

	/**
	 * 添加餐桌
	 * <ul>
	 * 	<li>判断是否有同名餐桌</li>
	 * 	<li></li>
	 * </ul>
	 *
	 * @param table
	 */
	@ApiOperation(value = "新增餐桌", notes = "新增餐桌")
	@ApiImplicitParam(name = "table", value = "餐桌实体", required = true, dataType = "Table")
	@RequestMapping(value = "/addTable", method = {RequestMethod.POST})
	public ReturnBean<Table> addTable(@RequestBody Table table){
		ReturnBean<Table> returnBean;
		String errorMessage = TableValidator.validateAddTableParams(table);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iTableService.addTable(table);
		}
		return returnBean;
	}

	/**
	 * 将状态置为无效
	 *
	 * @param tableID
	 */
	@ApiOperation(value = "删除餐桌", notes = "删除餐桌")
	@ApiImplicitParam(name = "tableID", value = "餐桌编码", required = true, dataType = "String", paramType = "form")
	@RequestMapping(value = "/delTable", method = {RequestMethod.POST})
	public ReturnBean<String> delTable(String tableID){
		ReturnBean<String> returnBean;
		String errorMessage = TableValidator.validateDelTableParams(tableID);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iTableService.delTable(tableID);
		}
		return returnBean;
	}

	/**
	 * 获取所有餐桌不分页
	 * <ul>
	 * 	<li>status=1 获取所有有效餐桌</li>
	 * 	<li>status=0 获取所有无效餐桌</li>
	 * 	<li>status=-1 获取所有餐桌</li>
	 * 	<li></li>
	 * </ul>
	 *
	 * @param status
	 */
	@ApiOperation(value = "获取所有餐桌不分页", notes = "获取所有餐桌不分页")
	@ApiImplicitParam(name = "status", value = "状态", required = true, dataType = "int", paramType = "query")
	@RequestMapping(value = "/getAllTable", method = {RequestMethod.GET})
	public ReturnBean<List<Table>> getAllTable(Integer status){
		ReturnBean<List<Table>> returnBean;
		String errorMessage = TableValidator.validateGetAllTableParams(status);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iTableService.getAllTable(status);
		}
		return returnBean;
	}

	/**
	 * 更新餐桌
	 * <ul>
	 * 	<li>判断是否有同名餐桌</li>
	 * 	<li></li>
	 * </ul>
	 *
	 * @param table
	 */
	@ApiOperation(value = "更新餐桌", notes = "更新餐桌")
	@ApiImplicitParam(name = "table", value = "餐桌实体", required = true, dataType = "Table")
	@RequestMapping(value = "/updateTable", method = {RequestMethod.POST})
	public ReturnBean<Table> updateTable(@RequestBody Table table){
		ReturnBean<Table> returnBean;
		String errorMessage = TableValidator.validateUpdateTableParams(table);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iTableService.updateTable(table);
		}
		return returnBean;
	}

	/**
	 * 更新餐桌状态
	 * <ul>
	 * 	<li>判断餐桌状态是否为要修改的状态的上个状态，是则可以修改，否则不可以修改</li>
	 * 	<li></li>
	 * </ul>
	 *
	 * @param tableID
	 * @param tableStatus
	 */
	@ApiOperation(value = "更新餐桌状态", notes = "更新餐桌状态")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "tableID", value = "餐桌ID", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "tableStatus", value = "餐桌状态", required = true, dataType = "int",
					allowableValues = "10,20,30,40,50", paramType = "form")}
	)
	@RequestMapping(value = "/updateTableStatus", method = {RequestMethod.POST})
	public ReturnBean<Table> updateTableStatus(String tableID, Integer tableStatus){
		ReturnBean<Table> returnBean;
		String errorMessage = TableValidator.validateUpdateTableStatusParams(tableID, tableStatus);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iTableService.updateTableStatus(tableID, tableStatus);
		}
		return returnBean;
	}

}
